package com.hlhk.kmlparserforandroid.kml;

import android.graphics.Color;


import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolygonOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.hlhk.kmlparserforandroid.kml.models.Point;
import com.hlhk.kmlparserforandroid.kml.reader.KmlReader;
import com.hlhk.kmlparserforandroid.kml.shapes.Placemark;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class KMLReaderUtils {

    public void readKMLFile(AMap aMap, String path) {
        KmlReader reader = new KmlReader();
        File dir = new File(path);
        reader.read(dir.getPath());
        readKMLPoints(aMap, reader);
        readKMLPolyLines(aMap, reader);
        readKMLPolygon(aMap, reader);
    }

    private void readKMLPoints(AMap aMap, KmlReader reader) {
        List<KmlShape> points = reader.getPoints();
        if (points != null && points.size() > 0) {
            //获取第一个点，将地图显示拉到当前多边形
            Placemark p0 = ((Placemark) points.get(0));
            double[] newP0 = Utils.wgs84_To_Gcj02(p0.mCoordinate.longitude, p0.mCoordinate.latitude);
            assert newP0 != null;
            LatLng latLng = new LatLng(newP0[1], newP0[0]);
            aMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
            for (int i = 0; i < points.size(); i++) {
                Placemark placemark = (Placemark) points.get(i);
                Point p = placemark.mCoordinate;
                //修正显示偏差
                double[] newLon = Utils.wgs84_To_Gcj02(p.longitude, p.latitude);
                assert newLon != null;
                LatLng l = new LatLng(newLon[1], newLon[0]);
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(l);
                markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                aMap.addMarker(markerOptions);
            }
        }
    }

    private void readKMLPolyLines(AMap aMap, KmlReader reader) {
        List<KmlShape> polyLines = reader.getPolylines();
        if (polyLines != null && polyLines.size() > 0) {
            //获取第一个点，将地图显示拉到当前多边形
            List<Point> p0 = ((com.hlhk.kmlparserforandroid.kml.shapes.Polyline) polyLines.get(0)).mCoordinates;
            double[] newP0 = Utils.wgs84_To_Gcj02(p0.get(0).longitude, p0.get(0).latitude);
            assert newP0 != null;
            LatLng latLng = new LatLng(newP0[1], newP0[0]);
            aMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
            for (int i = 0; i < polyLines.size(); i++) {
                com.hlhk.kmlparserforandroid.kml.shapes.Polyline polyline = (com.hlhk.kmlparserforandroid.kml.shapes.Polyline) polyLines.get(i);
                List<Point> linePoint = polyline.mCoordinates;
                Polyline mapPolyline = null;
                ArrayList<LatLng> latLngList = new ArrayList<>();
                for (int j = 0; j < linePoint.size(); j++) {
                    Point p = linePoint.get(j);
                    //修正显示偏差
                    double[] newLon = Utils.wgs84_To_Gcj02(p.longitude, p.latitude);
                    assert newLon != null;
                    latLngList.add(new LatLng(newLon[1], newLon[0]));
                    if (mapPolyline == null) {
                        mapPolyline = aMap.addPolyline((new PolylineOptions())
                                .addAll(latLngList)
                                .width(3)
                                .setDottedLine(false)
                                .color(Color.BLUE));
                    } else {
                        mapPolyline.setPoints(latLngList);
                    }
                }
            }
        }
    }

    private void readKMLPolygon(AMap aMap, KmlReader reader) {
        List<KmlShape> polygons = reader.getPolygons();
        if (polygons != null && polygons.size() > 0) {
            //获取第一个点，将地图显示拉到当前多边形
            List<Point> p0 = ((com.hlhk.kmlparserforandroid.kml.shapes.Polygon) polygons.get(0)).mCoordinates;
            double[] newP0 = Utils.wgs84_To_Gcj02(p0.get(0).longitude, p0.get(0).latitude);
            assert newP0 != null;
            LatLng latLng = new LatLng(newP0[1], newP0[0]);
            aMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
            //开始画多边形
            for (int i = 0; i < polygons.size(); i++) {
                ArrayList<LatLng> latLngList = new ArrayList<>();
                List<Point> pointList = ((com.hlhk.kmlparserforandroid.kml.shapes.Polygon) polygons.get(i)).mCoordinates;
                for (int j = 0; j < pointList.size(); j++) {
                    Point p = pointList.get(j);
                    //修正显示偏差
                    double[] newLon = Utils.wgs84_To_Gcj02(p.longitude, p.latitude);
                    assert newLon != null;
                    LatLng l = new LatLng(newLon[1], newLon[0]);
                    latLngList.add(l);
                }
                aMap.addPolygon(new PolygonOptions()
                        .addAll(latLngList)
                        .strokeWidth(3)
                        .strokeColor(Color.RED)
                        .fillColor(Color.argb(1, 1, 1, 1)));
            }
        }
    }

}
